package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @Discription:
 */
public class ParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-------------------doget--------------------");
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        String hobby = req.getParameter("hobby");//当有多个值使用这个方法只能获取一个
        String[] hobbies = req.getParameterValues("hobby");

        System.out.println("用户名"+username);
        System.out.println("密码"+password);
//        System.out.println("兴趣爱好"+ Arrays.toString(hobbies));//输出字符串打印
        System.out.println("兴趣爱好"+Arrays.asList(hobbies));//转为数组打印
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
        req.setCharacterEncoding("UTF-8");
        System.out.println("-------------------dopost--------------------");
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        String hobby = req.getParameter("hobby");//当有多个值使用这个方法只能获取一个
        String[] hobbies = req.getParameterValues("hobby");

        System.out.println("用户名"+username);
        System.out.println("密码"+password);
//        System.out.println("兴趣爱好"+ Arrays.toString(hobbies));//输出字符串打印
        System.out.println("兴趣爱好"+Arrays.asList(hobbies));//转为数组打印

    }
}
